//
//  LRXSettingController.m
//  路人侠
//
//  Created by zhangxulong on 4/10/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXSettingController.h"
#import "LRXSettingCell.h"
#import "AppDelegate.h"
#import "simpleNetTask.h"
#import "LRXOffline.h"
#import "LRXFixAounctPaswordView.h"

@interface LRXSettingController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSString * userModel;

@end

@implementation LRXSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:244 green:244 blue:244];
    
    self.userModel = [[NSUserDefaults standardUserDefaults] objectForKey:@"ident"];;
    self.navigationItem.title = @"设置";
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"首页" style:UIBarButtonItemStylePlain target:self action:@selector(popSettingView)];
    [self layoutContentView];
    
    
}

- (void)layoutContentView
{
    CGRect tableViewFrame = CGRectMake(0, 20, MainScreenW, self.view.frame.size.height - 20 * hScale);
    UITableView * tableView = [[UITableView alloc]initWithFrame:tableViewFrame style:UITableViewStyleGrouped];
    tableView.backgroundColor = [UIColor colorWithRed:244 green:244 blue:244];
    tableView.rowHeight = 80 * hScale;
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.scrollEnabled = NO;
    
    [self.view addSubview:tableView];
    
}
#pragma mark - tableView的代理方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * LRXSETTINGCELL = @"LRXSETTINGCELL";
    LRXSettingCell * cell = [tableView dequeueReusableCellWithIdentifier:LRXSETTINGCELL];
    if (cell == nil) {
        cell = [[LRXSettingCell alloc]init];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSRange  range = {3,4};
    NSString * userPhone = [self.userModel stringByReplacingCharactersInRange:range withString:@"****"];
    if (indexPath.section == 0) {
        cellSelectBlock block = ^{
            [LRXOfflineView showOfflineView:@"程序员正在努力加班中...."];
        };
        cell.selectBlock = block;
  
        [cell layoutCellView:@"phone" withTitle:userPhone withAss:@"bi"];
    }
    if (indexPath.section == 1 ) {
        if (indexPath.row == 0) {
            cellSelectBlock block = ^{
                [LRXOfflineView showOfflineView:@"程序员正在努力加班中...."];
                
            };
            cell.selectBlock = block;
            [cell layoutCellView:@"phoneNum" withTitle:userPhone withAss:@"bi"];
        }
        if (indexPath.row == 1) {
            cellSelectBlock block = ^{
                [LRXOfflineView showOfflineView:@"程序员正在努力加班中...."];
            };
            cell.selectBlock = block;
            [cell layoutCellView:@"password" withTitle:@"设置登录密码" withAss:@"bi"];
        }
    }
    
    if (indexPath.section == 2) {
        cellSelectBlock block = ^{
            
            CABasicAnimation * rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; //让其在z轴旋转
            rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 4.0 ];//旋转角度
            rotationAnimation.duration = 1; //旋转周期
            rotationAnimation.cumulative = YES;//旋转累加角度
            rotationAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
            [cell.cellAss.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
            
            
            simpleNetTaskCallBackBlock callBlock = ^(NSDictionary * responseObject){
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [LRXOfflineView showOfflineView:@"已是最新版本"];
                });
                
            };
            simpleNetTask * dataTask = [[simpleNetTask alloc]initWithLoadDataBlock:callBlock];
            [dataTask sessionDataTask:nil withUrl:syncUrl withMethod:@"GET"];
        };
        cell.selectBlock = block;
        [cell layoutCellView:@"update" withTitle:@"检查新版本" withAss:@"update2"];
    }
   
    return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return 1;
    }
    else{
        return 2;
    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel * headLabel = [[UILabel alloc]init];
    if (section == 0) {
        headLabel.text = @"     基本设置";
    }
    else if (section == 1) {
        headLabel.text = @"     账号设置";
    }
    else
    {
        headLabel.text = @"     其他";
    }
    headLabel.font = [UIFont systemFontOfSize:24 * hScale];
    headLabel.textColor = mainColor;
    
    return headLabel;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LRXSettingCell * cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectBlock();
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60 * hScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0 || section == 1) {
        return 40 * hScale;
    }
    return 150 * hScale;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MainScreenW, 150 * hScale)];
        footView.backgroundColor = [UIColor clearColor];
        
        UIButton * cancelbtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelbtn.frame = CGRectMake(60 * wScale, 50 * hScale, MainScreenW - 120 * wScale, 70 * hScale);
        [cancelbtn setTitle:@"退出帐号" forState:UIControlStateNormal];
        cancelbtn.backgroundColor = [UIColor colorWithRed:220 green:20 blue:60];
        cancelbtn.layer.cornerRadius = 10 *hScale;
        [cancelbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        cancelbtn.titleLabel.font = [UIFont systemFontOfSize:30 * hScale];
        [cancelbtn addTarget:self action:@selector(cancelButton) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:cancelbtn];
        
        
        return footView;
    }
    return nil;
}

- (void)cancelButton
{
    
    NSLog(@"%s",__func__);
    
    
    //注销
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    [manger POST:logoutUrl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSArray * cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
        for (id obj in cookies) {
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:obj];
        }
        
        //删除归档文件
        NSFileManager *defaultManager = [NSFileManager defaultManager];
        if ([defaultManager isDeletableFileAtPath:userFilePath]) {
            [defaultManager removeItemAtPath:userFilePath error:nil];
        }
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"ident"];
        //注销用户身份
        
        AppDelegate * delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.userRole = USEROFFLINETYPE;
        
        [[LRXMenuView shareInstance] cancelButtonPressed];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
   
}


- (void)popSettingView
{
    NSLog(@"%s",__func__);
    [self.navigationController popToRootViewControllerAnimated:YES];
}





@end
